﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ChatData;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace WCFServiceWebRole1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
  //  [ServiceBehavior(InstanceContextMode =
  //InstanceContextMode.Single)]
    public class ChatServer : IChatServer
    {
        public CloudStorageAccount acc = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
        TheServer theserver;

        public ChatServer()
        {
            theserver = new TheServer(acc);
        }
        public TheServer Server
        {
            get { return theserver; }
            set { theserver = value; }

        }
        #region IChat Members

        bool IChatServer.UpdateStatus(string user, string status)
        {
            Request newrequest = new Request(RequestType.Status, user);
            newrequest.Status = status;
            Console.WriteLine("Handing status update from " + user);
            theserver.HandleRequest(newrequest);
            return true;
        }
        bool IChatServer.Post(string originIP, string user, string status, string message)
        {
            Request newrequest = new Request(RequestType.Message, user);
            newrequest.IP = originIP;
            newrequest.Status = status;
            newrequest.Mesage = message;
            newrequest.Time = DateTime.Now;
            Console.WriteLine("Handing new Post from " + user);
            theserver.HandleRequest(newrequest);
            return true;
        }

        bool IChatServer.DM(string originIP, string user, string status, string message, string destination)
        {
            Request newrequest = new Request(RequestType.DM, user);
            newrequest.IP = originIP;
            newrequest.Time = DateTime.Now;
            newrequest.Mesage = message;
            newrequest.Destination = destination;
            newrequest.Username = user;
            Console.WriteLine("Handing a Direct message from " + user + " to " + destination);
            theserver.HandleRequest(newrequest);
            return true;
        }
        void IChatServer.ForwardFile(string user, string destination, string GUID)
        {

            Request newrequest = new Request(RequestType.File, user);
            newrequest.Username = user;
            newrequest.GUID = GUID;
            newrequest.Destination = destination;
            Console.WriteLine(user + " Fowarded this file " + GUID + " to " + destination);
            theserver.HandleFileRequest(newrequest);
            // no need to reverse
        }
        FilePost IChatServer.RequestFile(String user, String GUID)
        {
            Request newrequest = new Request(RequestType.GetFile, user);
            newrequest.GUID = GUID;
            Console.WriteLine(user + " Requested this file " + GUID);
            FilePost thepost = theserver.HandleFileRequest(newrequest);
            Array.Reverse(thepost.File);// for security
            return thepost;
        }
        bool IChatServer.PostFile(string origin, string user, string status, byte[] file, string filename, string destination)
        {
            Array.Reverse(file);//reset original byte order
            Request newrequest = new Request(RequestType.File, user);
            newrequest.IP = origin;
            newrequest.Status = status;
            newrequest.FileName = filename;
            newrequest.File = file;
            newrequest.Destination = destination;
            Console.WriteLine(user + " Sent a file  to " + destination);
            theserver.HandleFileRequest(newrequest);
            return true;
        }

        bool IChatServer.Login(string IP, string port, string username, string password)
        {
            Request newrequest = new Request(RequestType.Login, username);
            newrequest.IP = IP;
            newrequest.Port = port;
            newrequest.Password = password;
            Console.WriteLine(username + " Logged in");
            return theserver.HandleRequest(newrequest);
        }

        void IChatServer.Logout(string user)
        {
            Request newrequest = new Request(RequestType.Logoff, user);
            Console.WriteLine(user + " Logged out ");
            theserver.HandleRequest(newrequest);
        }

        List<DMPost> IChatServer.getDMStream(string user)
        {
            Request newrequest = new Request(RequestType.GetDMStream, user);
            return theserver.HandleDMStreamRequest(newrequest);
        }

        List<DMPost> IChatServer.getDMStreamSince(string user, DateTime time)
        {
            Request newrequest = new Request(RequestType.GetDMStreamSince, user);
            newrequest.Time = time;
            List<DMPost> temp = theserver.HandleDMStreamRequest(newrequest);
            if (temp != null)
            {
                return temp;
            }
            else return new List<DMPost>();
        }

        List<Post> IChatServer.getStream(string user)
        {
            Request newrequest = new Request(RequestType.GetStream, user);
            List<Post> temp = theserver.HandleStreamRequest(newrequest);
            if (temp != null)
            {
                return temp;
            }
            else return new List<Post>();
        }

        List<Post> IChatServer.getUpdatedStream(string user, DateTime time)
        {
            Request newrequest = new Request(RequestType.GetStreamSince, user);
            newrequest.Time = time;
            return theserver.HandleStreamRequest(newrequest);
        }

        List<Record> IChatServer.getActiveUsers()
        {

            return theserver.HandleUserRequest();
        }

        #endregion
    }
}
